package org.example.caiwumanage.service.impl;



import org.example.caiwumanage.entity.Employee;
import org.example.caiwumanage.mapper.EmployeeMapper;
import org.example.caiwumanage.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(Employee employeeLoginDTO) {
        // 更换MD5加密后的密码
        String md5Password = DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes());
        employeeLoginDTO.setPassword(md5Password);
        // 查询数据库中是否存在该用户并比对密码是否正确
        Employee employee=employeeMapper.selectbyusername(employeeLoginDTO);
        if(employee==null) {
            employeeLoginDTO.setPhone(employeeLoginDTO.getUsername());
            employee=employeeMapper.selectbyphone(employeeLoginDTO);
        }
        return employee;
    }

    @Override
    public void register(Employee employee) {
        //MD5加密后的密码
        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(md5Password);

        employeeMapper.register(employee);
    }

    @Override
    public Employee perfectList(long id) {
        return employeeMapper.perfectlist(id);
    }

    @Override
    public void perfect(Employee employee) {
        employeeMapper.perfect(employee);
    }

    @Override
    public void forgotPassword(Employee employee) {
        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(md5Password);
        employeeMapper.resetPassword(employee);
    }


}